package pers.cnzdy.tutorial.Chapter5_UI.Section7_ListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import pers.cnzdy.tutorial.R;


public class QuizTypesAdapter extends ArrayAdapter<QuizTypes.QuizType> {
    private int resourceId;

    public QuizTypesAdapter(Context context, int textViewResourceId, List<QuizTypes.QuizType> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        QuizTypes.QuizType quizType = getItem(position);

        View view;
        ViewHolder viewHolder;

        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.quizIcon = view.findViewById(R.id.quiz_icon);
            viewHolder.quizType = view.findViewById(R.id.quiz_type);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder)view.getTag();
        }

        assert quizType != null;
        viewHolder.quizIcon.setImageResource(quizType.getImageID());
        viewHolder.quizType.setText(quizType.getType());

        //-------------------------------//
        // 性能较差
//        view = LayoutInflater.from(getContext()).inflate(resourceId, null);
//		ImageView quizImage = (ImageView)view.findViewById(R.id.quiz_icon);
//		TextView quizTypeText = (TextView)view.findViewById(R.id.quiz_type);
//
//		quizImage.setImageResource(quizType.getImageID());
//        quizTypeText.setText(quizType.getType());
        //-------------------------------//

        return view;
    }

    class ViewHolder {
        ImageView quizIcon;
        TextView  quizType;
    }
}
